<template>
	<view>
		<block v-for="(item,idx) in services" :key="idx">
			<zc-card :zcIndex="idx" :cardItem="item"  @zcChengeValue="zcChangeValue" @zcChengeIcon="zcChengeIcon" ></zc-card>
		</block>
		
		<view class="Custom_picker_mask" @touchmove.stop="zcTouchmove" v-show="isShowMask">
			<view class="Custom_picker_content">
				<view class="Custom_picker_title">赠送项目<view class="iconfont icon-shibai1"></view></view>
				<view class="Custom_picker_box">
					<view class="">
						<block v-for="(item,index) in ServerProjects" :key="index">
							<view class="Custom_picker_item uni-flex zcAlignI_center zcJustifyC_between" @tap="doServerProject(item,index)">
								<view class="">{{item.Name}}</view>
								<view class="" style="color: #3CA4F9;" v-show="item.GiftTypeName" >[ {{item.GiftTypeName}} ]</view>
							</view>
						</block>
					</view>
				</view>
			</view>
		</view>
		<view class="zcFooter-but uni-flex">
			<button @tap="zcRescue">重置</button>
			<button @tap="zcSaveInsurePolicyGift">提交</button>
		</view>
	</view>
</template>

<script>
	import zcCard from "../../components/zc-card/zc-card.vue"
	
	let _this;
	export default {
		components:{
			zcCard
		},
		data() {
			return {
				InsurePolicySN:'',	// 保单SN
				InsureSubmitDate:'',	// 保单提交时间
				MainOrganSN:'',	//主体
				ServerProject:'',	//服务项目 参数
				ServerProjects:[],	// 服务项目列表
				isShowMask:false,
				GiftSN:'',	// 赠品SN
				services:[
					{
						title:'赠送时间',
						value:'',
						isTitleColon:true,
						type:"input",
						isDisabled:true,
						width:30,
						inputStyle:{	//input输入框样式
							placeholderText:"",		//默认显示内容
							type:"text",	//input输入框拉起键盘类型：text（拼音键盘） , number（数字键盘） , idcard（身份证雅正键盘）
						},
						rStyle:{type:""}
					},
					{
						title:'赠送项目',
						value:'',
						id:0,
						isTitleColon:true,
						type:"input",
						width:30,
						inputStyle:{	//input输入框样式
							placeholderText:"请输入查询赠品、类别名称",		//默认显示内容
							type:"text",	//input输入框拉起键盘类型：text（拼音键盘） , number（数字键盘） , idcard（身份证雅正键盘）
						},
						rStyle:{type:"uniIcon",color:"#3CA4F9",fontSize:30,iconName:'search'}
					},
					{
						title:'赠送数量',
						value:'',
						isTitleColon:true,
						type:"inputChange",
						width:30,
						inputStyle:{	//input输入框样式
							placeholderText:"",		//默认显示内容
							type:"number",	//input输入框拉起键盘类型：text（拼音键盘） , number（数字键盘） , idcard（身份证雅正键盘）
						},
						rStyle:{type:""}
					},
					{
						title:'成本金额',
						value:'',
						isTitleColon:true,
						type:"input",
						isDisabled:true,
						width:30,
						inputStyle:{	//input输入框样式
							placeholderText:"",		//默认显示内容
							type:"number",	//input输入框拉起键盘类型：text（拼音键盘） , number（数字键盘） , idcard（身份证雅正键盘）
						},
						rStyle:{type:""}
					},
					{
						title:'面值金额',
						value:'',
						isTitleColon:true,
						type:"input",
						isDisabled:true,
						width:30,
						inputStyle:{	//input输入框样式
							placeholderText:"",		//默认显示内容
							type:"number",	//input输入框拉起键盘类型：text（拼音键盘） , number（数字键盘） , idcard（身份证雅正键盘）
						},
						rStyle:{type:""}
						
					},
					{
						title:'是否收银支付',
						value:'',
						isTitleColon:true,
						type:"checkbox",
						isDisabled:true,
						width:30,
						optionBoxs:[	//选项框 样式
							{
								text:"",	//显示值
								value:"是否收银支付",	//隐藏值
								boxSize:0.5,	// 范围：0.5~1
								isBoxCheck: false,	//是否选中
								isBoxDisabled:false,		//是否禁止
								boxColor:"#FFFFFF"	//选中后样式颜色
							}
						],
						rStyle:{type:""}
					},
					{
						title:'项目详情',
						value:'',
						isTitleColon:true,
						isTCenter:true,
						type:"textarea",
						width:30,
						inputStyle:{	//input输入框样式
							placeholderText:"",		//默认显示内容
							isAutoHeight:true,
							type:"text",
						},
						rStyle:{type:""}
					}
				],
				GiftList:[]
			}
		},
		onLoad(e) {
			_this = this;
			
			if (e.InsurePolicySN) {
				_this.InsurePolicySN = e.InsurePolicySN;
			}
			if (e.InsureSubmitDate) {
				_this.InsureSubmitDate = e.InsureSubmitDate;
			}
			if (e.MainOrganSN) {
				_this.MainOrganSN = e.MainOrganSN;
			}
			_this.zcInsurePolicyGiftCreateNewSN();	// 获取 保存SN
			let date = new Date();
			_this.services[0].value = date.toJSON().split('T')[0];
		},
		methods: {
			zcTouchmove() {
				return;
			},
			zcChangeValue(res) {
				_this.services[res.id].value = res.value;
				if (res.id === 1) {
					if (_this.ServerProject !== res.value) {
						_this.services[3].value = '';
						_this.services[4].value = '';
						_this.services[5].optionBoxs[0].isBoxCheck = false;
					}
					_this.ServerProject = res.value;
				}
				if (res.id === 2) {
					if (_this.services[1].value) {
						_this.services[3].value = parseFloat(_this.GiftList[parseInt(_this.services[1].id)].Cost)*parseInt(res.value);
						_this.services[4].value = parseFloat(_this.GiftList[parseInt(_this.services[1].id)].Price)*parseInt(res.value);
					}
				}
			},
			//  赠送项目 列表接口
			zcChengeIcon(res) {
				setTimeout(()=>{
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					_this.$ZcPublic.Server.Post({
						IsNeedLogin: true,
						Paras: {
							Name: 'uni_Insure_Detail',
							Action: 'Ext_QueryNonStopGiftList',
							Data: {
								MainOrganSN:_this.MainOrganSN,
								Name:_this.ServerProject
							}
						},
						Callback:function(resjson,scope) {
							if (resjson.state === 'back') {
								console.log('服务赠送 项目 = ',resjson.data);
								if (!resjson.success) {
									uni.showModal({
										title:'温馨提示',
										showCancel:false,
										content:''+resjson.msg
									})
									return;
								}
								_this.ServerProjects = [];
								if (resjson.data.length > 0) {
									_this.GiftList = resjson.data;
									_this.isShowMask = true;
									
									for (let i = 0; i < resjson.data.length; i++) {
										_this.ServerProjects.push(resjson.data[i]);
									}
								}else{
									uni.showModal({
										title: '温馨提示',
										content: '尊敬的用户，没有找到相关服务项目信息！',
										showCancel: false
									});
								}
							}
                            if (resjson.state === 'end') {
                                uni.hideLoading();
                            }
							
						},
						Scope: this
					})
				},500)
			},
			doServerProject(item,index) {
				_this.services[1].value = item.Name;
				_this.services[1].id = index;
				if (_this.services[2].value) {
					_this.services[3].value = parseFloat(item.Cost)*parseInt(_this.services[2].value);
					_this.services[4].value = parseFloat(item.Price)*parseInt(_this.services[2].value);
				}else{
					_this.services[2].value = 1;
					_this.services[3].value = item.Cost;
					_this.services[4].value = item.Price;
				}
				_this.isShowMask = false;
				_this.services[5].optionBoxs[0].isBoxCheck = item.isCashDelivery;
			},
			zcRescue() {	// 重置操作
				setTimeout(()=>{
					for (let i = 1; i < _this.services.length; i++) {
						_this.services[i].value = '';
					}
					_this.services[5].optionBoxs[0].isBoxCheck = false;
					uni.showToast({
						title: '重置成功！'
					});
				},100)
			},
			//  获取 保存SN
			zcInsurePolicyGiftCreateNewSN() {
				_this.$ZcPublic.Server.Post({
					IsNeedLogin: true,
					Paras: {
						Name: 'uni_Insure_Detail',
						Action: 'Ext_InsurePolicyGiftCreateNewSN',
						Data: {}
					},
					Callback:function(resjson,scope) {
						if (resjson.state === 'back') {
							console.log('服务赠送 保存SN = ',resjson.data);
							if (!resjson.success) {
								uni.showModal({
									title:'温馨提示',
									showCancel:false,
									content:''+resjson.msg
								})
								return;
							}
							
							if (resjson.data) {
								_this.GiftSN = resjson.data;
							}
						} else{
							
						}
						
					},
					Scope: this
				})
			},
			//  服务赠送 新增保存
			zcSaveInsurePolicyGift() {
				let date = new Date();
				
				date = date.toJSON().split('T')[0];
				if (_this.InsureSubmitDate) {
					if (_this.InsureSubmitDate.split('-')[1] !== date.split('-')[1]) {
						uni.showModal({
							title: '温馨提示',
							content: '尊敬的用户，赠送活动已结束！',
							showCancel: false
						});
						return;
					}
				}
				console.log('暗森 --- ',_this.services);
				if (!_this.services[4].value) {
					uni.showModal({
						title: '温馨提示',
						content: '尊敬的用户，请查询选择赠送项目！',
						showCancel: false
					});
					return;
				}
				setTimeout(()=>{
					let InsurePolicyGift = {
						SN:_this.GiftSN,
						InsurePolicySN:_this.InsurePolicySN,
						GiftSN:_this.GiftList[parseInt(_this.services[1].id)].SN,
						CreateDate:_this.services[0].value,
						GiftTypeSN:_this.GiftList[parseInt(_this.services[1].id)].GiftTypeSN,
						GiftTypeName:_this.GiftList[parseInt(_this.services[1].id)].GiftTypeName,
						GiftName:_this.GiftList[parseInt(_this.services[1].id)].Name,
						Count:_this.services[2].value,
						GiftCost:_this.GiftList[parseInt(_this.services[1].id)].Cost,
						GiftPrice:_this.GiftList[parseInt(_this.services[1].id)].Price,
						GiftTotalCost:_this.services[3].value,
						GiftTotalMoney:_this.services[4].value,
						isCashDelivery:_this.services[5].optionBoxs[0].isBoxCheck,
						GiftComments:_this.services[6].value
						
					};
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					_this.$ZcPublic.Server.Post({
						IsNeedLogin: true,
						Paras: {
							Name: 'uni_Insure_Detail',
							Action: 'Ext_SaveInsurePolicyGift',
							Data: InsurePolicyGift
						},
						Callback:function(resjson,scope) {
							if (resjson.state === 'back') {
								if (!resjson.success) {
									uni.showModal({
										title:'温馨提示',
										showCancel:false,
										content:''+resjson.msg
									})
									return;
								}
								
								uni.showToast({
									title: '提交成功!',
									success() {
										uni.navigateBack({
											delta:1 
										});
									}
								});
							} else{
								
							}
						},
						Scope: this
					})
				},100);
			},
			
		}
	}
</script>

<style>
	.card_item .card_item_text input{text-align: right;}
	.card_item .card_item_text>picker .uni-input{position: relative;z-index: 6;padding: 0;padding-right: 50upx;text-align: right;}
	.card_item .card_item_text>picker .uni-input:after{
		font-family: "iconfont" !important;
		font-size: 30upx;
		font-style: normal;
		font-weight: bold;
		position: absolute;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
		content: '\e600';
		top: 0;
		right: 0upx;
		transform: rotate(270deg);
	}
	.card_item .card_item_text textarea{height: 50upx;min-height: 50upx;}
	.zcAlignI_center1{justify-content: flex-end;}
	.card_item_name>view{line-height: 1!important;}
	.zcAlignI_center{justify-content: flex-end;}
	.uni-icon-search{padding: 0 20upx;font-size: 28upx;height: 100%;display: flex;align-items: center;color: #3CA4F9!important;}
	
	.Custom_picker_mask{width: 100%;height: 100%;background-color: rgba(0,0,0,0.2);position: fixed;top: 0;left: 0;display: flex;align-items: center;justify-content: center;z-index: 100;}
	.Custom_picker_content{height: 60%;width: 70%;background-color: #FFFFFF;border-radius: 10upx;}
	.Custom_picker_title{color: #3CA4F9;border-bottom: 4upx solid #F5F5F5;height: 10%;box-shadow: 0 0 6upx 0 #E5E5E5;display: flex;align-items: center;justify-content: center;position: relative;}
	.Custom_picker_box{height: 89%;width: 100%;overflow: auto;box-sizing: border-box;}
	.Custom_picker_item{margin: 0 30upx;border-bottom: 3upx solid #F5F5F5;padding: 6upx 20upx;}
	.Custom_picker_item:hover{background-color: #3CA4F9;color: #FFFFFF;}
	/* .icon-shibai1{position: absolute;right: 10upx;height: 60upx;width: 60upx;display: flex;align-items: center;justify-content: center;} */
	
	.zcFooter-but{position: fixed;bottom: 0;left: 0;width: 100%;z-index: 10;background-color: #F4F5F6!important;align-items: center;}
	.zcFooter-but button:first-child{flex: 1;color: #333333;border-radius: 0;background-color: #FFFFFF;font-size: 32upx;padding: 10upx;}
	.zcFooter-but button:last-child{flex: 1;background-color: rgba(60, 164, 250, 1);color: #FFFFFF;border-radius: 0;font-size: 32upx;padding: 10upx;}
	.zcFooter-but .button-hover{opacity: 0.6;}
</style>
